/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.springboot.jeesite.modules.sys.service;

import com.springboot.jeesite.common.persistence.Page;
import com.springboot.jeesite.common.quartz.QuartzManager;
import com.springboot.jeesite.common.service.CrudService;
import com.springboot.jeesite.modules.sys.dao.TaskDao;
import com.springboot.jeesite.modules.sys.entity.Task;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 定时任务配置Service
 *
 * @author xxx
 * @version 2019-01-14
 */
@Service
@Transactional(readOnly = true)
public class TaskService extends CrudService<TaskDao, Task> {

    @Autowired
    QuartzManager quartzManager;

    public Task get(String id) {
        return super.get(id);
    }

    public List<Task> findList(Task task) {
        return super.findList(task);
    }

    public Page<Task> findPage(Page<Task> page, Task task) {
        return super.findPage(page, task);
    }

    @Transactional(readOnly = false)
    public void save(Task task) {
        super.save(task);
    }

    @Transactional(readOnly = false)
    public void delete(Task task) {
        super.delete(task);
    }


    public void initSchedule() throws SchedulerException {
        // 这里获取任务信息数据
        List<Task> taskList = super.findList(new Task());
        for (Task task : taskList) {
            if ("1".equals(task.getJobStatus())) {
                quartzManager.addJob(task);
            }

        }
    }

    /**
     * 停用启用定时任务
     *
     * @param id
     * @param cmd
     * @throws SchedulerException
     */
    @Transactional(readOnly = false)
    public void changeStatus(String id, String cmd) throws SchedulerException {
        Task task = get(id);
        if (task == null) {
            return;
        }
        if ("stop".equals(cmd)) {
            quartzManager.deleteJob(task);
            task.setJobStatus("0");
        } else {
            if (!"start".equals(cmd)) {
            } else {
                task.setJobStatus("1");
                quartzManager.addJob(task);
            }
        }
        save(task);
    }

    /**
     * 更新cron表达式
     *
     * @param id
     * @throws SchedulerException
     */
    @Transactional(readOnly = false)
    public void updateCron(String id) throws SchedulerException {
        Task task = get(id);
        if (task == null) {
            return;
        }
        if ("1".equals(task.getJobStatus())) {
            quartzManager.updateJobCron(task);
        }
        save(task);
    }

    /**
     * 立即执行job
     *
     * @param id
     * @throws SchedulerException
     */
    @Transactional(readOnly = false)
    public void runAJobNow(String id) throws SchedulerException {
        Task task = get(id);
        if (task == null) {
            return;
        }
        if ("1".equals(task.getJobStatus())) {
            quartzManager.runAJobNow(task);
        }
    }

}